<template>
    <div>
        <!-- 表格 -->
        <el-table :data="userList" border stripe>
            <el-table-column type="index" label="序号" width="90"></el-table-column>
            <el-table-column prop="name" label="姓名"></el-table-column>
            <el-table-column prop="phone" label="电话"></el-table-column>
            <el-table-column prop="address" label="地址"> </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
export default {
    data() {
        return {
            // 用户列表
            userList: [],
            // 总条数
            total: 0
        }
    },
    created() {
        this.loadUsers()
    },
    methods: {
        // 加载用户列表
        async loadUsers() {
            const { data: res } = await this.$http.get('/users/queryByPage')
            if (res.status !== 200) {
                return this.$message.error(res.msg)
            }
            this.userList = res.data.records
            this.total = res.data.total
        },
        // 编辑用户
        handleEdit(index, row) {
            console.log(index, row);
        },
        // 删除用户
        handleDelete(id) {
            this.$confirm('确认要删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const { data: res } = await this.$http.delete(`users/remove/${id}`)
                if (res.status !== 200) {
                    return this.$message.error(res.msg)
                }
                this.loadUsers()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });


        }
    }
}
</script>

<style lang="less" scoped></style>